const db = require('../plugin/query');

const saveArticle = (obj) => {
    const { id, title, content, author, type, createTime, editTime, status, cover, recentReadingTime, isDelete, isPrivate } = obj;
    let keys = `id, `;
    let values = `'${id}', `;
    if (title) {
        keys = `${keys}title, `
        values = `${values}'${title}', `;
    }
    if (content) {
        keys = `${keys}content, `
        values = `${values}'${content}', `;
    }
    if (author) {
        keys = `${keys}author, `
        values = `${values}'${author}', `;
    }
    if (type) {
        keys = `${keys}type, `
        values = `${values}'${type}', `;
    }
    if (createTime) {
        keys = `${keys}createTime, `
        values = `${values}'${createTime}', `;
    }
    if (editTime) {
        keys = `${keys}editTime, `
        values = `${values}'${editTime}', `;
    }
    if (status) {
        keys = `${keys}status, `
        values = `${values}'${status}', `;
    }
    if (cover) {
        keys = `${keys}cover, `
        values = `${values}'${cover}', `;
    }
    if (recentReadingTime) {
        keys = `${keys}recentReadingTime, `
        values = `${values}'${recentReadingTime}', `;
    }
    if (isDelete) {
        keys = `${keys}isDelete, `
        values = `${values}'${isDelete}', `;
    }
    if (isPrivate) {
        keys = `${keys}isPrivate, `
        values = `${values}'${isPrivate}', `;
    }
    keys = keys.replace(/, $/, '');
    values = values.replace(/, $/, '');
    // 动态构建SQL语句
    let sql = `INSERT INTO article (${keys}) VALUES (${values})`;
    try {
        db.query(sql);
        return {
            code: 1,
            msg: '新增成功'
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const editArticle = (obj) => {
    const { id, title, content, author, type, createTime, editTime, status, cover, recentReadingTime, isPrivate } = obj;
    let sqlClause = `recentReadingTime = '${recentReadingTime}',`;
    if (title) {
        sqlClause = `${sqlClause}title = '${title}',`;
    }
    if (content) {
        sqlClause = `${sqlClause}content = '${content}',`;
    }
    if (author) {
        sqlClause = `${sqlClause}author = '${author}',`;
    }
    if (type) {
        sqlClause = `${sqlClause}type = '${type}', `;
    }
    // if (createTime) {
    //     sqlClause = `${sqlClause}createTim = ${createTime}, `;
    // }
    if (editTime) {
        sqlClause = `${sqlClause}editTime = '${editTime}', `;
    }
    if (status) {
        sqlClause = `${sqlClause}status = '${status}', `;
    }
    if (cover) {
        sqlClause = `${sqlClause}cover = '${cover}', `;
    }
    if (isPrivate) {
        sqlClause = `${sqlClause}isPrivate = '${isPrivate}', `;
    }
    sqlClause = sqlClause.replace(/, $/, '');
    // 动态构建SQL语句
    let sql = `UPDATE article SET ${sqlClause} WHERE id = "${id}"`;
    try {
        db.query(sql);
        return {
            code: 1,
            msg: '更新成功'
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const getArticleList = async (obj) => {
    try {
        const { type, status } = obj;
        let sql = `SELECT * FROM article WHERE isDelete = 1 AND `;
        if (type) {
            sql = `${sql}type = ${type} AND `;
        }
        if (status) {
            sql = `${sql}status = ${status}`;
        }
        const [rows, fields] = await db.query(`${sql} order by editTime desc`);
        return {
            code: 1,
            msg: '查询成功',
            data: rows,
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const getArticle = async (id) => {
    let sql = `SELECT *, DATE_FORMAT(createTime, '%Y-%m-%d %H:%i:%s') as cDate FROM article WHERE id = "${id}"`;
    try {
        const [rows, fields] = await db.query(sql);

        if (!rows.length) {
            return {
                code: 1,
                msg: '查询失败，找不到文章',
            }
        }
        return {
            code: 1,
            msg: '查询成功',
            data: rows,
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const setRecentReadingTime = async (id, recentReadingTime) => {
    try {
        let sql = `UPDATE article SET recentReadingTime = ${recentReadingTime} WHERE id = "${id}"`;
        db.query(sql);
        return {
            code: 1,
            msg: '更新成功'
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const getRecentReadingList = async () => {
    try {
        let sql = `select * from article where isDelete = 1 order by recentReadingTime desc limit 10`;
        const [rows, fields] = await db.query(sql);
        return {
            code: 1,
            msg: '查询成功',
            data: rows
        }
    } catch (err) {
        console.error(err);
        return {
            code: -1,
            msg: 'err:' + err
        }
    }
}

const deleteArticle = (id) => {
    try {
        let sql = `update article SET isDelete = 0 WHERE id = "${id}"`;
        db.query(sql);
        return {
            code: 1,
            msg: '删除成功'
        }
    } catch (err) {
        console.log(err);
    }
}

module.exports = {
    saveArticle,
    editArticle,
    getArticleList,
    getArticle,
    setRecentReadingTime,
    getRecentReadingList,
    deleteArticle,
}